
package com.waymap.utils;


import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.SignatureType;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;



public class NSRestConnector {


	public  String DATA_SYNC_REST;

	private String TOKEN_ID;

	private String TOKEN_SECRET;

	private String CONSUMER_KEY ;

	private String CONSUMER_SECRET;

	private String REALM ;

	private  String REST_URL;

	private String CONTENT_TYPE="content-type";

	private String APP_JSON="application/json";

	private String EMPTY_JSON_PAYLOAD="{}";

	private OAuthService service ;
	private Token accessToken;

	public NSRestConnector() {}
	public NSRestConnector(String dATA_SYNC_REST, String tOKEN_ID, String tOKEN_SECRET, String cONSUMER_KEY,
			String cONSUMER_SECRET, String rEALM) {
		super();
		DATA_SYNC_REST = dATA_SYNC_REST;
		TOKEN_ID = tOKEN_ID;
		TOKEN_SECRET = tOKEN_SECRET;
		CONSUMER_KEY = cONSUMER_KEY;
		CONSUMER_SECRET = cONSUMER_SECRET;
		REALM = rEALM;
		service = getService();
		accessToken = getToken();
		REST_URL = DATA_SYNC_REST;
	}


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NSRestConnector ta = new NSRestConnector();
	//	Response response = ta.callWithHttpGet(REST_URL);
	//	Response response = ta.callWithHttpPost();
	//	System.out.println(response.getBody());
	}
	
	public Response callWithHttpGet(String url){
		OAuthRequest request = new OAuthRequest(Verb.GET, url);
		request.setRealm(REALM);
		request.addHeader(CONTENT_TYPE, APP_JSON);
		service.signRequest(accessToken, request);
		System.out.println(request.getHeaders());
		return request.send();
	}
	
	private Response callWithHttpPost() {
		OAuthRequest request = new OAuthRequest(Verb.POST, REST_URL);
		request.setRealm(REALM);
		request.addHeader(CONTENT_TYPE, APP_JSON);
		request.addPayload(EMPTY_JSON_PAYLOAD);
		service.signRequest(accessToken, request);
		return request.send();
	}
	
	public  Response callWithHttpPost(String payload) {
		OAuthRequest request = new OAuthRequest(Verb.POST, REST_URL);
		request.setRealm(REALM);
		request.addHeader(CONTENT_TYPE, APP_JSON);
		request.addPayload(payload);
		service.signRequest(accessToken, request);
		return request.send();
	}
	
	private Token getToken() {
		return new Token(TOKEN_ID, TOKEN_SECRET);
	}
	
	private OAuthService getService() {
		return new ServiceBuilder()
				.provider(DummyService.class)
				.apiKey(CONSUMER_KEY)
				.apiSecret(CONSUMER_SECRET)
				.signatureType(SignatureType.Header)
				.build();
	}

	public String getREST_URL() {
		return REST_URL;
	}

	public void setREST_URL(String rEST_URL) {
		REST_URL = rEST_URL;
	}




	public void setTOKEN_ID(String TOKEN_ID) {
		this.TOKEN_ID = TOKEN_ID;
	}

	public void setTOKEN_SECRET(String TOKEN_SECRET) {
		this.TOKEN_SECRET = TOKEN_SECRET;
	}

	public void setCONSUMER_KEY(String CONSUMER_KEY) {
		this.CONSUMER_KEY = CONSUMER_KEY;
	}

	public void setCONSUMER_SECRET(String CONSUMER_SECRET) {
		this.CONSUMER_SECRET = CONSUMER_SECRET;
	}

	public void setREALM(String REALM) {
		this.REALM = REALM;
	}


	public String getDATA_SYNC_REST() {
		return DATA_SYNC_REST;
	}

	public String getTOKEN_ID() {
		return TOKEN_ID;
	}

	public String getTOKEN_SECRET() {
		return TOKEN_SECRET;
	}

	public String getCONSUMER_KEY() {
		return CONSUMER_KEY;
	}

	public String getCONSUMER_SECRET() {
		return CONSUMER_SECRET;
	}

	public String getREALM() {
		return REALM;
	}

	public void setDATA_SYNC_REST(String DATA_SYNC_REST) {
		this.DATA_SYNC_REST = DATA_SYNC_REST;
	}
}
